package org.cladwell;

import com.mysql.cj.util.StringUtils;
import lombok.Data;
import org.Util.MD5Util;
import org.Util.RSAUtil;
import org.Util.TkoenUtil;
import org.cladwell.constant.UserConstant;
import org.cladwell.domain.User;
import org.cladwell.domain.Userinfo;
import org.cladwell.exception.ConditionException;
import org.cladwell.mapper.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserService {
    @Autowired
    private UserDao userDao;

    public  User getUserInfo(Long userid) {
        Userinfo userinfo=userDao.getUserinfo(userid);

        User user= userDao.getUserbyid(userid);
        user.setUserinfo(userinfo);
        return user;

    }

    public void addUser(User user){
        String phone = user.getPhone();
        if(StringUtils.isNullOrEmpty(phone)){
            throw  new ConditionException("手机号不能为空");
        }
        User user1=this.getUserByPhne(phone);
        if(user1!=null){
            throw  new ConditionException("手机号已经注册了");
        }
        Date now=new Date();
        String salt=String.valueOf(now.getTime());
        String password = user.getPassword();
        String RawPassword;
        try {
            RawPassword= RSAUtil.decrypt(password);
        } catch (Exception e) {
            throw new ConditionException("密码解密失败");
        }
        MD5Util.sign(RawPassword,salt,"UTF-8");
        user.setSalt(salt);
        user.setPassword(RawPassword);
        user.setCreateTime(now);
        userDao.adduser(user);
        Userinfo userinfo=new Userinfo();
        userinfo.setUserId(user.getId());
//        还会有系统的预设的也要加上去
        userinfo.setNick(UserConstant.neck);
        userinfo.setBirth(UserConstant.BIRTH);
        userinfo.setGender(UserConstant.GENDER_UNKNOW);
        userinfo.setCreateTime(now);
        userDao.adduserinfo(userinfo);
    }
    public User getUserByPhne(String phone){
        return userDao.getUserByname(phone);
    }

    public String login(User user) throws Exception {
        String phone = user.getPhone();
        if(StringUtils.isNullOrEmpty(phone)){
            throw  new ConditionException("手机号为空");
        }
        User userByPhne = this.getUserByPhne(phone);
        if(userByPhne==null){
            throw  new ConditionException("当前用户不存在");
        }
        String password = user.getPassword();
        try {
            String decrypt = RSAUtil.decrypt(password);
        } catch (Exception e) {
            throw new ConditionException("密码解释失败");
        }
        String salt=userByPhne.getSalt();
        String sign = MD5Util.sign(password, salt, "UTF-8");
        if(!sign.equals(userByPhne.getPassword())){
            throw new ConditionException("密码错误");
        }
       return  TkoenUtil.generateToken(userByPhne.getId());
    }

    public void updatauserinfo(Userinfo userinfo) {
        userinfo.setUpdataTime(new Date());
        userDao.updataUserinfo(userinfo);
    }


    public User getUserById(Long id) {
        return userDao.getUserbyid(id);
    }
}
